<?php

namespace App\Listeners;

use App\Events\RechargeBillRecord;
use App\Repositories\Eloquent\FinanceBillRepository;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class RechargeBillRecordListener implements ShouldQueue
{

    public $queue = 'bill_record';
    public $tries = 3;
    public $billRepo;

    /**
     * Create the event listener.
     *
     * @param FinanceBillRepository $billRepository
     */
    public function __construct(FinanceBillRepository $billRepository)
    {
        //
        $this->billRepo = $billRepository;
    }

    /**
     * Handle the event.
     *
     * @param \App\Events\RechargeBillRecord $event
     *
     * @return void
     */
    public function handle(RechargeBillRecord $event)
    {
        //如果是商户
        $this->billRepo->createMchROBillRecord($event->order);
        if ($event->order->proxy) {
            $this->billRepo->createProxyROBillRecord($event->order);
        }
    }

    /**
     * 处理任务失败
     *
     * @param RechargeBillRecord $event
     * @param  \Exception $exception
     * @return void
     */
    public function failed(RechargeBillRecord $event, $exception)
    {
        //
        Log::info('交易完成订单流水写入失败! 交易订单号['.$event->order->plat_no .']，错误信息:'.$exception->getMessage());
    }
}
